package models;

import java.util.*;

import javax.persistence.*;

import play.db.jpa.*;

/**
 * @author Mostafa Fahim
 * @version 1.0
 */
@Entity
public class ActionItem extends Model {
	@Lob
	public String description;
	public double status;
	public Date startDate;
	public Date endDate;
	public EEntity entity;
	@ManyToOne
	public Plan plan;
	@ManyToMany
	public List<User> users;
	@ManyToMany
	public List<Tag> tags;
	@ManyToMany
	public List<Topic> topics;
	@OneToMany
	public List<Request> requestsToJoin;

	/**
	 * @author Mostafa Fahim
	 * 
	 * @param description
	 *            the description of the work that will be done during execution
	 *            of this action item
	 * @param startDate
	 *            the date at which the action item will start execution
	 * @param endDate
	 *            the date at which the action item execution will end
	 */

	public ActionItem(String description, Date startDate, Date endDate) {
		this.description = description;
		this.status = 0.0;
		this.startDate = startDate;
		this.endDate = endDate;
		this.entity = null;
		users = new ArrayList<User>();
		tags = new ArrayList<Tag>();
		this.topics = new ArrayList<Topic>();
		this.requestsToJoin = new ArrayList<Request>();
		this.save();
	}

	/*
	 * A method which sets the plan this item is related to
	 * 
	 * @param planId the id of the plan this item should be related to
	 * 
	 * @return the according ActionItem object
	 */

	public ActionItem setPlan(long planId) {
		Plan plan = Plan.findById(planId);
		this.plan = plan;
		this.save();
		return this;
	}

	/*
	 * A method which sets the entity this item is related to
	 * 
	 * @param planId the id of the entity this item should be related to
	 * 
	 * @return the according ActionItem object
	 */

	public ActionItem setEntity(long entityId) {
		EEntity entity = EEntity.findById(entityId);
		this.entity = entity;
		this.save();
		return this;
	}

	/*
	 * A method which adds a topic this item is related to
	 * 
	 * @param topicId the id of the topic this item should be related to
	 * 
	 * @return the according ActionItem object
	 */

	public ActionItem addTopic(long topicId) {
		Topic topic = Topic.findById(topicId);
		this.topics.add(topic);
		this.save();
		return this;
	}

	/*
	 * A method which adds a topic this item is related to
	 * 
	 * @param topicId the id of the topic this item should be related to
	 * 
	 * @return the according ActionItem object
	 */

	public ActionItem addTag(long tagId) {
		Tag tag = Tag.findById(tagId);
		this.tags.add(tag);
		this.save();
		return this;
	}

	/**
	 * formats the start date of the action item like yy-mm-dd
	 * 
	 * @author Mostafa Fahim
	 * 
	 * @return the formated string of the start date
	 */

	public String formatStartDate() {
		if (startDate == null)
			return null;
		return startDate.toString().substring(0, 10);
	}

	/**
	 * formats the start date of the action item like yyyymmdd specifically for
	 * the calendar API used in showing the related plan
	 * 
	 * @author Mostafa Fahim
	 * 
	 * @return the required formatted string of the start date
	 */

	public String formatStartDateforCalendar() {
		String formatted = formatStartDate();
		if (formatted == null)
			return null;
		String[] splitted = formatted.split("-");
		return splitted[0] + splitted[1] + splitted[2];

	}

	// Mostafa
	public Plan getPlan() {
		return plan;
	}

	/**
	 * formats the end date of the action item like yy-mm-dd
	 * 
	 * @author Mostafa Fahim
	 * 
	 * @return the formated string of the end date
	 */
	public String formatEndDate() {
		if (endDate == null)
			return null;
		return endDate.toString().substring(0, 10);
	}

	/**
	 * formats the end date of the action item like yyyymmdd specifically for
	 * the calendar API used in showing the related plan
	 * 
	 * @author Mostafa Fahim
	 * 
	 * @return the required formatted string of the end date
	 */
	public String formatEndDateforCalendar() {
		String formatted = formatEndDate();
		if (formatted == null)
			return null;
		String[] splitted = formatted.split("-");
		return splitted[0] + splitted[1] + splitted[2];
	}

	/**
	 * @author Ahmed Mahmoud
	 * @return
	 */
	public boolean cascadeDelete() {
		try {
			if (entity != null && entity.items != null)
				entity.items.remove(this);
			plan.actionItems.remove(this);

			for (User user : users) {
				user.actionItems.remove(this);
				user.save();
			}

			for (Tag tag : tags) {
				tag.relatedActionItems.remove(this);
				tag.save();
			}
			for (Topic topic : topics) {
				topic.items.remove(this);
				topic.save();
			}
			this.delete();
			System.out.println("hkjhj");
		} catch (Exception ex) {
			System.out.println("hkll");
			return false;
		}
		return true;
	}

	/**
	 * Returns a substring of the description of the action item with length
	 * maximum 40 chars
	 * 
	 * @author Mostafa Fahim
	 * @return String a substring of the description of the plan
	 */
	public String getSummarizedContent() {
		if (description.length() < 40)
			return description;
		else
			return description.substring(0, 40) + "...";
	}
}
